<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\common\model\Article as ArticleModel;
use app\common\model\Category as CategoryModel;
use app\common\model\Apply as ApplyModel;
use app\common\model\User as UserModel;

class Apply extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ApplyModel();
        $first_category_name = 'column';
        $this->CategoryModel = new CategoryModel();
        $category_tree = $this->CategoryModel->treeView(true);
        if(isset($category_tree[$first_category_name])){
          $tree_node = $category_tree[$first_category_name];
        }else{
          $tree_node = $this->CategoryModel->emptyTreeRootNode();
        }
        $this->tree_node = $tree_node;
        $this->assign('active_nav', $first_category_name);
    }

    public function index()
    {
        $this->redirect('personal/index');
    }

    public function read($id = 0)
    {
        if(empty($this->current_user)){
            $this->redirect('index/index');
        }
        $apply = $this->Model->userFind($id);
        if(empty($apply)){
            $this->redirect('index');
        }

        // 设置页面SEO信息
        $this->setPageSeo([
            'title' => '我的申报',
        ]);

        // $ArticleModel = new ArticleModel();
        // $article = $ArticleModel->where('id', $apply['article_id'])->find();
        // if(!empty($article)){
        //     // 设置页面SEO信息
        //     $this->setPageSeo([
        //         'title' => $article['title'],
        //         'description' => $article['seo_description'],
        //         'keywords' => $article['seo_keywords'],
        //     ]);
        // }else{
        //     // 设置页面SEO信息
        //     $this->setPageSeo([
        //         'title' => $apply['title'],
        //     ]);
        // }

        $this->assign('apply', $apply);
        return $this->fetch();
    }

    // 申报项目
    public function create(){
        $article_id = intval($this->request->param('article_id'));

        // 只有status=1的article可以申报
        $article = $this->findValidArticle($article_id, ['view_mode' => '']);

        $old_apply = $this->Model->findUserArticleItem($article_id);

        $redirect_url = NULL;
        $redirect_msg = "";
        if(empty($this->current_user)){
            $redirect_msg = "请先登录";
            if(empty($article)){
                $redirect_url = url('column/index');
            }else{
                $redirect_url = url('column/read', ['id' => $article_id]);
            }
        }else if(!empty($old_apply)){
            $redirect_msg = "您已成功申报该项目";
            $redirect_url = url('read', ['id' => $old_apply['id']]);
        }else if(empty($article)){
            $redirect_msg = "您申报的项目不存在";
            $redirect_url = url('column/index');
        }else if(!$article['second_category_applied']){
            $redirect_msg = "您申报的项目不存在";
            $redirect_url = url('column/read', ['id' => $article_id]);
        }

        if($this->request->isPost()){
            if(!is_null($redirect_url)){
                if($this->request->isAjax()){
                    $this->error($redirect_msg, $redirect_url);
                }else{
                    $this->redirect($redirect_url);
                }
            }

            $fields = [
                ['name' => 'title', 'type' => 'string', 'default' => ''],
                ['name' => 'user_name', 'type' => 'string', 'default' => 0],
                ['name' => 'birthday', 'type' => 'birthday', 'is_null' => true],
                ['name' => 'id_num', 'type' => 'string', 'default' => ''],
                ['name' => 'gender', 'type' => 'string', 'default' => ''],
                ['name' => 'education', 'type' => 'string', 'default' => ''],
                ['name' => 'pro_title', 'type' => 'string', 'default' => ''],
                ['name' => 'email', 'type' => 'string', 'default' => ''],
                ['name' => 'mobile', 'type' => 'string', 'default' => ''],
                ['name' => 'college_name', 'type' => 'string', 'default' => ''],
                ['name' => 'college_type', 'type' => 'string', 'default' => ''],
                ['name' => 'college_no', 'type' => 'string', 'default' => ''],
                ['name' => 'college_depart', 'type' => 'string', 'default' => ''],
                ['name' => 'attachment_path', 'type' => 'string', 'default' => ''],
                ['name' => 'attachment_name', 'type' => 'string', 'default' => ''],
            ];
            $default = [
                'article_id' => $article_id,
            ];
            $data = $this->parsePostParam($fields, $default);
            $res = $this->Model->createOrUpdate($data);
            if($res["status"]){
                $id = 0;
                if(isset($res['data']) && isset($res['data']['id'])){
                    $id = $res['data']['id'];
                }
                $this->success($res["msg"], url('read', ['id' => $id]));
            }else{
                $this->error($res["msg"]);
            }
        }else{
            if(!is_null($redirect_url)){
                $this->redirect($redirect_url);
            }

            $gender_list = ['male' => '男', 'female' => '女'];
            $this->assign('gender_list', $gender_list);
            $this->assign('article', $article);

            $clone_fields = [
                'name' => 'user_name',
                'mobile' => 'mobile',
                'email' => 'email',
                'birthday' => 'birthday',
                'gender' => 'gender',
                'college_name' => 'college_name',
                'pro_title' => 'pro_title',
            ];
            $UserModel = new UserModel;
            $user_detail = $UserModel->currentDetail();
            $apply = [];
            if(empty($user_detail)){
                foreach ($clone_fields as $key => $name) {
                    $apply[$name] = "";
                }
            }else{
                foreach ($clone_fields as $key => $name) {
                    $apply[$name] = $user_detail[$key];
                }
            }
            $this->assign('apply', $apply);

            // // 设置页面SEO信息
            // $this->setPageSeo([
            //     'title' => $article['title'],
            //     'description' => $article['seo_description'],
            //     'keywords' => $article['seo_keywords'],
            // ]);

            // 设置页面SEO信息
            $this->setPageSeo([
                'title' => '申报项目',
            ]);
            return $this->fetch();
        }
    }
}
